package com.tencent.ncnnyolox.basemodule.view

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import com.tencent.ncnnyolox.R
import com.tencent.ncnnyolox.basemodule.base.BaseDialog
import com.tencent.ncnnyolox.basemodule.base.action.AnimAction
import com.tencent.ncnnyolox.net.bean.PhraseLabelBean

/**
 * <pre>
 *     author : yh
 *     e-mail : xxx@xx
 *     time   : 2023/12/29
 *     desc   :
 * </pre>
 */
class CommonEditDialog {

    @Suppress("UNCHECKED_CAST", "LeakingThis")
    open class Builder(context: Context) : BaseDialog.Builder<Builder>(context) {

        private var autoDismiss = true

        private val containerLayout: ViewGroup? by lazy { findViewById(R.id.ll_ui_container) }
        private val titleView: TextView? by lazy { findViewById(R.id.tv_ui_title) }
        private val cancelView: TextView? by lazy { findViewById(R.id.tv_ui_cancel) }
        private val lineView: View? by lazy { findViewById(R.id.v_ui_line) }
        private val confirmView: TextView? by lazy { findViewById(R.id.tv_ui_confirm) }
        private val llButton: LinearLayout? by lazy { findViewById(R.id.ll_button) }

        private val edText:EditText? by lazy { findViewById(R.id.ed_content) }
        private val label1:TextView? by lazy { findViewById(R.id.label1) }
        private val label2:TextView? by lazy { findViewById(R.id.label2) }
        private val  label3:TextView? by lazy { findViewById(R.id.label3) }
        private lateinit var bean:PhraseLabelBean
        private lateinit var listLabel:List<PhraseLabelBean>

        init {
            setContentView(R.layout.dialog_common_word)
            setAnimStyle(AnimAction.ANIM_BOTTOM)
            setGravity(Gravity.CENTER)
            setOnClickListener(cancelView, confirmView)
            cancelView?.setOnClickListener {
                listener?.onCancel(getDialog())
            }
            label1?.setOnClickListener{
                label1!!.setBackgroundResource(R.drawable.bg_label_checked)
                label1!!.setTextColor(context.resources.getColor(R.color.white))
                label2!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label2!!.setTextColor(context.resources.getColor(R.color.common_666))
                label3!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label3!!.setTextColor(context.resources.getColor(R.color.common_666))
                bean=listLabel[0]
            }
            label2?.setOnClickListener{
                label1!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label1!!.setTextColor(context.resources.getColor(R.color.common_666))
                label2!!.setBackgroundResource(R.drawable.bg_label_checked)
                label2!!.setTextColor(context.resources.getColor(R.color.white))
                label3!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label3!!.setTextColor(context.resources.getColor(R.color.common_666))
                bean=listLabel[1]
            }

            label3?.setOnClickListener{
                label1!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label1!!.setTextColor(context.resources.getColor(R.color.common_666))
                label2!!.setBackgroundResource(R.drawable.bg_label_unchecked)
                label2!!.setTextColor(context.resources.getColor(R.color.common_666))
                label3!!.setBackgroundResource(R.drawable.bg_label_checked)
                label3!!.setTextColor(context.resources.getColor(R.color.white))
                bean=listLabel[2]
            }


            confirmView?.setOnClickListener {

               listener?.onConfirm(getDialog(),edText!!.text.toString(),bean)

            }

        }

        fun setContent(text:String):Builder=apply {
            edText?.setText(text)
        }

        fun setLabel(list: List<PhraseLabelBean>):Builder=apply{
            listLabel=list
            label1!!.setText(list[0].labelName)
            label2!!.setText(list[1].labelName)
            label3!!.setText(list[2].labelName)
        }




        fun setCustomView(@LayoutRes id: Int): Builder =apply {
            return setCustomView(LayoutInflater.from(getContext()).inflate(id, containerLayout, false))
        }

        fun setCustomView(view: View?): Builder =apply {
            containerLayout?.addView(view, 1)
        }

        fun setTitle(@StringRes id: Int): Builder =apply {
            return setTitle(getString(id))
        }

        fun setTitle(text: CharSequence?): Builder =apply {
            titleView?.text = text

        }

        fun setTitleColor(color:Int): Builder =apply {
            titleView?.setTextColor(color)
        }

        fun setCancel(@StringRes id: Int): Builder =apply {
            return setCancel(getString(id))
        }

        fun setCancel(text: CharSequence?): Builder =apply{
            cancelView?.text = text
            lineView?.visibility = if (text == null || "" == text.toString()) View.GONE else View.VISIBLE
        }

        fun setConfirm(@StringRes id: Int): Builder =apply {
            return setConfirm(getString(id))
        }

        fun setConfirmColor(color:Int): Builder =apply {
            confirmView?.setTextColor(color)
        }

        fun setConfirm(text: CharSequence?): Builder =apply {
            confirmView?.text = text

        }

        fun setAutoDismiss(dismiss: Boolean): Builder =apply {
            autoDismiss = dismiss
        }

        fun autoDismiss() {
            if (autoDismiss) {
                dismiss()
            }
        }

        private var listener: OnListener? = null
        fun setListener(listener: OnListener?): Builder = apply {
            this.listener = listener
        }
        interface OnListener {

            /**
             * 点击确定时回调
             */
            fun onConfirm(
                dialog: BaseDialog?, text:String,bean:PhraseLabelBean)

            /**
             * 点击取消时回调
             */
            fun onCancel(dialog: BaseDialog?) {

            }
        }
    }
}